package kvoog.arkanoid.rendering;

import kvoog.arkanoid.core.Health;
import kvoog.arkanoid.core.Player;

import com.artemis.Aspect;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.EntitySystem;
import com.artemis.annotations.Mapper;
import com.artemis.managers.PlayerManager;
import com.artemis.utils.ImmutableBag;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class HudRenderingSystem extends EntitySystem {
	
	private @Mapper ComponentMapper<Player> plm;
	private @Mapper ComponentMapper<Health> hlm;
	
	private BitmapFont font;
	private OrthographicCamera camera;
	private SpriteBatch spriteBatch;

	@SuppressWarnings("unchecked")
	public HudRenderingSystem(SpriteBatch spriteBatch) {
		super(Aspect.getAspectForAll(Player.class, Health.class));
		this.spriteBatch = spriteBatch;
	}

	@Override
	protected void initialize() {
		super.initialize();
		
		Texture fontTexture = new Texture(Gdx.files.internal("fonts/normal_0.png"));
        fontTexture.setFilter(TextureFilter.Linear, TextureFilter.MipMapLinearLinear);
        TextureRegion fontRegion = new TextureRegion(fontTexture);
        font = new BitmapFont(Gdx.files.internal("fonts/normal.fnt"), fontRegion, false);
        font.setUseIntegerPositions(false);
		
		camera = new OrthographicCamera(800, 480);
		camera.update();
	}

	@Override
	protected void processEntities(ImmutableBag<Entity> entities) {
		
		spriteBatch.setProjectionMatrix(camera.combined);
		spriteBatch.enableBlending();
		spriteBatch.begin();
		
		
		Entity e = world.getManager(PlayerManager.class).getEntitiesOfPlayer("player1").get(0);
		Player player = plm.get(e);
		Health health = hlm.get(e);
		
		spriteBatch.setColor(1, 1, 1, 1);
		String scoreTxt = "SCORE: " + player.score;	
		font.draw(spriteBatch, scoreTxt, 270, 220);
		String healthTxt = "LIVES: " + health.health;
		font.draw(spriteBatch, healthTxt, -370, 220);
				
		spriteBatch.end();
	}
	
	@Override
	protected boolean checkProcessing() {
		return true;
	}

}
